package com.session06.am.cainiao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ManageService extends JFrame {

    private JPanel p1, p2;
    private JLabel area;
    private JTextField txtInput;
    private JButton btn1, btn2;
    String old = "";

    public ManageService() {

    }

    public ManageService(Login login) {
        Staff[] staff = login.getStaff();

        old = new String(login.getTxtPwd().getPassword());

        Operate operate = new Operate();

        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String txtNum = txtInput.getText();
                switch (txtNum) {
                    case "1":
                        showInfor(staff);
                        txtInput.setText("");
                        break;
                    case "2":
                        add(staff);
                        txtInput.setText("");
                        break;
                    case "3":
                        delete(staff);
                        txtInput.setText("");
                        break;
                    case "4":
                        modify(staff);
                        txtInput.setText("");
                        break;
                    case "5":
                        operate.dispose();
                        txtInput.setText("");
                        //此步骤可以将bounder界面显示出来
                        //login.boundar.setVisible(true);

                        //显示欢迎界面
                        Welcome welcome = new Welcome();
                }
            }

        });
    }

    private void showInfor(Staff[] staff) {
        String inFor = "姓名     密码     等级\n";
        int len = 0;
        for (int i = 0; i < staff.length; i++) {
            if (staff[i] != null) {
                len++;
            }
        }
        for (int i = 0; i < len; i++) {
            inFor += staff[i].getUsername() + "     " + staff[i].getPassword() + "     " + staff[i].getLevel() + "\n";
        }
        JOptionPane.showMessageDialog(null, inFor);
    }

    private void add(Staff[] staff) {
        int index = -1;
        for (int i = 0; i < staff.length; i++) {
            if (staff[i] == null) {
                index = i;
                break;
            }
        }
        String str1 = JOptionPane.showInputDialog(null, "请输入要添加的名字:");
        String str2 = JOptionPane.showInputDialog(null, "请输入员工的等级(管理员/员工)");
        if (index != -1) {
            staff[index] = new Staff(str1, "", str2);
            JOptionPane.showMessageDialog(null, "添加成功");
        }
    }

    private void delete(Staff[] staff) {
        String str = JOptionPane.showInputDialog(null, "请输入你的名字:");
        boolean flag = false;
        for (int i = 0; i < staff.length; i++) {
            if (staff[i] != null) {
                if (staff[i].getUsername().equals(str)) {
                    int j = i;
                    while (staff[j + 1] != null) {
                        staff[j].setUsername(staff[j + 1].getUsername());
                        staff[j].setLevel(staff[j + 1].getLevel());
                        j++;
                    }
                    //最后一本书
                    staff[j] = null;
                    flag = true;
                }
            }
        }
        if (flag) {
            JOptionPane.showMessageDialog(null, "删除成功！");
        } else {
            JOptionPane.showMessageDialog(null, "查无此人");
        }
    }

    public void modify(Staff[] staff) {
        String pwd1 = JOptionPane.showInputDialog(null, "请输入旧密码");
        int num1 = -1;
        for (int i = 0; i < staff.length; i++) {
            if (staff[i] != null) {
                if (pwd1.equals(staff[i].getPassword())) {
                    num1 = i;
                    break;
                }
            }
        }
        if (num1 != -1) {
            String pwd2 = JOptionPane.showInputDialog(null, "请输入新密码");
            String pwd3 = JOptionPane.showInputDialog(null, "请确认新密码");
            if (pwd3.equals(pwd2)) {
                JOptionPane.showConfirmDialog(null, "修改成功");
                staff[num1].setPassword(pwd2);
                old = pwd2;
            } else {
                JOptionPane.showConfirmDialog(null, "两次密码不一致");
            }
        }
    }

    public class Operate extends JFrame {
        public Operate() {
            this.setTitle("管理员界面");
            this.setBounds(300, 100, 350, 250);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            p1 = new JPanel();
            area = new JLabel("<html>欢迎使用员工管理<br><br>" +
                    "1、查看所有员工<br>2、添加员工<br>3、删除员工<br>4、修改密码<br>5、安全退出</html>");
            p1.add(area);
            txtInput = new JTextField();
            txtInput.setPreferredSize(new Dimension(240, 20));
            p1.add(txtInput);
            this.add(p1);

            p2 = new JPanel();
            btn1 = new JButton("确认");
            btn2 = new JButton("取消");
            p2.add(btn1);
            p2.add(btn2);
            this.add(p2, BorderLayout.SOUTH);

            this.setVisible(true);
        }
    }
}
